//
// Created by 李传智 on 2018/5/1.
//

#include "IRecorder.h"

void IRecorder::update(VideoData data) {
    while (!isExist) {
        mutex.lock();
        if (pkts.size() <= maxPackets) {
            pkts.push_back(data);
            mutex.unlock();
            break;
        }
        mutex.unlock();
        sleep(1);
    }
}

void IRecorder::main() {
    while (!isExist) {
        mutex.lock();
        if (pkts.empty()) {
            mutex.unlock();
            sleep(1);
            continue;
        }
        VideoData packet = pkts.front();
        if (packet.pts > syncPts) {
            mutex.unlock();
            sleep(1);
            continue;
        }
        pkts.pop_front();
        this->record(packet);
        mutex.unlock();
    }
}

bool IRecorder::clear() {
//    mutex.lock();
//    while (!pkts.empty()) {
//        pkts.front().free();
//        pkts.pop_front();
//    }
//    syncPts = 0;
//    mutex.unlock();
}